package com.example.list;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class MyLinearLayout extends LinearLayout {
    Context mContext;
    public MyLinearLayout(Context context) {
        this(context, null);

    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, null, 0);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        setOrientation(LinearLayout.HORIZONTAL);
        init();
    }

    private void init() {
        LinearLayout.LayoutParams layoutParams_edt = null;

            layoutParams_edt = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams_edt.weight = 5;

        layoutParams_edt.setMargins(0, 0, 0, 0);
        layoutParams_edt.gravity = Gravity.CENTER_VERTICAL;
        EditText mEditText = new EditText(mContext);
        mEditText.setPadding(0, 0, 0, 0);
        mEditText.setBackgroundColor(Color.TRANSPARENT);

        mEditText.setGravity(Gravity.CENTER_VERTICAL);
        mEditText.setSingleLine(true);
        mEditText.setEllipsize(TextUtils.TruncateAt.END);
        mEditText.setBackgroundColor(Color.RED);
        addView(mEditText, layoutParams_edt);

        LinearLayout.LayoutParams layoutParams_img = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams_img.gravity = Gravity.CENTER_VERTICAL;
//        layoutParams_img.weight = 1;
//        if (!isLayoutWeight) {
//            // TODO: 2021/6/12
            layoutParams_img.setMargins(Dp2Px(mContext,10), 0,Dp2Px(mContext,10) , 0);
//        }

        ImageView mImageView = new ImageView(mContext);
        mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        mImageView.setImageResource(R.drawable.img_close);
        mImageView.setPadding(0,0,0,0);
        addView(mImageView, layoutParams_img);


//        Button mImageView = new Button(mContext);
//        mImageView.setBackgroundColor(Color.GREEN);
////        mImageView.setPadding(10,10,10,10);
//        addView(mImageView, layoutParams_img);
    }

    //        安卓中像素px和dp的转换：
    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density; //当前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public int Px2Dp(Context context, float px) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
